﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers.LopService;
using eTMS.Controllers;
using eTMS.Commons;
using eTMS.Controllers.NamHocService;
using eTMS.Controllers.NganhHocService;
using Button = eTMS.Controls.Button;

namespace eTMS.Forms.Students
{
    public partial class FrmLop : eTMS.Controls.BaseForm
    {
        public FrmLop()
        {
            InitializeComponent();
        }
        #region Variables
        private LopController controller = new LopController();
        private SinhVienController sinhVienController = new SinhVienController();
        #endregion
        #region Screen Functions
        //Load All lable text
        private void LoadLanguage()
        {
            this.Text = "Phân lớp sinh viên";
            this.labelFormTitle.Text = Constants.APP_ADMINISTRATION + " " + LopStringMapping.Lop;

        }
        //Get entity selected from datagrid
        private LopEntity GetObject()
        {
            LopEntity o = treeKhoaNganhKhoaHoc1.GetSelectedLop();
            return o;
        }


        private void LockScreen(bool IsLocked)
        {
            ucCommand.LockControl(IsLocked);
            txtGhiChu.ReadOnly = IsLocked;
            txtMaLop.ReadOnly = IsLocked;
            txtSoSVToiDa.ReadOnly = IsLocked;
            txtTenLop.ReadOnly = IsLocked;
        }
        #endregion
        #region Business

        //Delete data from database
        private bool DeleteData()
        {
            bool rval = true;
            try
            {
                foreach (LopEntity lopEntity in treeKhoaNganhKhoaHoc1.GetSelectedListLop())
                {
                    if (!controller.DeleteByID(lopEntity.Lop_ID))
                    {
                        rval = false;
                    }
                }
                treeKhoaNganhKhoaHoc1.RemoveSelectedListLop();
            }
            catch
            {
                return false;
            }
            return true;
        }
        #endregion
        #region DataBinding
        //Bind data to datagrid
        private void LoadData()
        {
            treeKhoaNganhKhoaHoc1.IsShowLop = true;
            sinhVienList1.SetController(sinhVienController);
            treeKhoaNganhKhoaHoc1.InitData();
        }

        #endregion

        #region Windows Control EventHandlers
        private eTMS.Controls.Button btnXoaSinhVienKhoiLop;
        private eTMS.Controls.Button btnChonSV;
        private void FrmLop_Load(object sender, EventArgs e)
        {
            treeKhoaNganhKhoaHoc1.IsShowSoSVToiDaLop = true;
            LoadLanguage();
            LoadData();
            LockScreen(true);
            btnChonSV = new Button();
            btnChonSV.Width = 80;
            btnChonSV.Text = "Chọn SV";
            btnChonSV.Click += new EventHandler(btnChonSV_Click);
            ucCommand.AddButton(btnChonSV);

            btnXoaSinhVienKhoiLop = new Button();
            btnXoaSinhVienKhoiLop.Width = 120;
            btnXoaSinhVienKhoiLop.Text = "Xóa SV khỏi lớp";
            btnXoaSinhVienKhoiLop.Click += new EventHandler(btnXoaSinhVienKhoiLop_Click);
            ucCommand.AddButton(btnXoaSinhVienKhoiLop);
        }

        void btnXoaSinhVienKhoiLop_Click(object sender, EventArgs e)
        {
            if (treeKhoaNganhKhoaHoc1.GetSelectedLop() ==null)
            {
                ShowInfo(Constants.MSS_CHOOSERECORD_REQUIRE);
                return;
            }
            if (Confirm("Bạn thực sự muốn xóa các sinh viên lựa chọn ra khỏi lớp hiện tại?"))
            {
                List<int> lsLopSinhVien_ID = new List<int>();
                foreach (DataRow row in sinhVienList1.GetSelectedSinhVienRows())
                {
                    lsLopSinhVien_ID.Add(sinhVienController.ConvertInt(row["LopSinhVien_ID"]));
                }
                LopSinhVienController lopSinhVienController = new LopSinhVienController();
                if (lopSinhVienController.DeleteByListLopSinhVien_ID(lsLopSinhVien_ID))
                {
                    BindSinhVienTrongLop();
                }
            }
        }

        void btnChonSV_Click(object sender, EventArgs e)
        {
            FrmChonSinhVienVaoLop frmChonSinhVienVaoLop = new FrmChonSinhVienVaoLop();
            frmChonSinhVienVaoLop.SetDataRowLop(treeKhoaNganhKhoaHoc1.GetSelectedListDataRowLop());
            frmChonSinhVienVaoLop.ShowDialog();
        }

        private void ucCommand_OnAddNew(object sender, EventArgs e)
        {
            NganhHocEntity nganhHocEntity = treeKhoaNganhKhoaHoc1.GetSelectedNganhHoc();
            FrmAddNewRangeLop frmAddNewRangeLop = new FrmAddNewRangeLop();
            KhoaController khoaController = new KhoaController();
            khoaController.LoadObject(nganhHocEntity.Khoa_ID);
            frmAddNewRangeLop.NganhHoc = nganhHocEntity;
            frmAddNewRangeLop.Khoa = khoaController.KhoaObject;
            frmAddNewRangeLop.KhoaHoc = treeKhoaNganhKhoaHoc1.GetKhoaHoc();
            NamHocEntity namHocEntity = NamHocController.GetCurrentNamHoc();
            if (namHocEntity != null)
            {
                frmAddNewRangeLop.MaNamHoc = namHocEntity.MaNamHoc.ToString();
            }
            frmAddNewRangeLop.SetController(controller);
            if (frmAddNewRangeLop.ShowDialog() == DialogResult.OK)
            {
                treeKhoaNganhKhoaHoc1.UpdateToScreen();
            }
        }
        private void ucCommand_OnDelete(object sender, EventArgs e)
        {
            if (treeKhoaNganhKhoaHoc1.GetSelectedListLop().Count == 0)
            {
                ShowInfo(Constants.MSS_CHOOSERECORD_REQUIRE);
                return;
            }
            if (!Confirm(Constants.MSS_CONFIRM_DELETE)) return;
            DeleteData();
        }
        private void ucCommand_OnEdit(object sender, EventArgs e)
        {
            LockScreen(false);
        }
        private void ucCommand_OnSave(object sender, EventArgs e)
        {
            controller.LopObject.MaLop = txtMaLop.Text;
            controller.LopObject.TenLop = txtTenLop.Text;
            controller.LopObject.SoSinhVienToiDa = controller.ConvertInt(txtSoSVToiDa.Text);
            controller.LopObject.MoTaLop = txtGhiChu.Text;
            if (controller.Update())
            {
                treeKhoaNganhKhoaHoc1.UpdateLopNodeText();
                LockScreen(true);
            }
            else
            {
                ShowWarning(Constants.MSS_SAVE_FAILE);
            }
        }
        private void ucCommand_OnSkip(object sender, EventArgs e)
        {
            LockScreen(true);
        }
        #endregion

        private void treeKhoaNganhKhoaHoc1_OnChangeKhoaHoc(object sender, EventArgs e)
        {
            ucCommand.AddNewButton.Enabled = (treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID > 0);
            if (btnChonSV != null)
                btnChonSV.Enabled = (treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID > 0) && treeKhoaNganhKhoaHoc1.GetSelectedListLop().Count > 0;
            controller.LopObject = treeKhoaNganhKhoaHoc1.GetSelectedLop();
            if (controller.LopObject != null)
            {
                txtMaLop.Text = controller.LopObject.MaLop;
                txtTenLop.Text = controller.LopObject.TenLop;
                txtSoSVToiDa.Text = controller.LopObject.SoSinhVienToiDa.ToString();
                txtGhiChu.Text = controller.LopObject.MoTaLop;
            }
            BindSinhVienTrongLop();
        }

        private void BindSinhVienTrongLop()
        {
            if (treeKhoaNganhKhoaHoc1.GetSelectedLop() == null) return;
            sinhVienController.ResetObject();
            sinhVienController.SinhVienObject.GioiTinh = 2;
            sinhVienController.SearchSinhVien(-1, -1, GetObject().Lop_ID, treeKhoaNganhKhoaHoc1.KhoaHoc_ID);
            sinhVienList1.UpdateToScreen();
        }
    }
}
